public class MySingleList {
    public MySingleList.ListNode head;

    public MySingleList() {
    }

    public void createList() {
        MySingleList.ListNode listNode1 = new ListNode(12);
        MySingleList.ListNode listNode2 = new ListNode(23);
        MySingleList.ListNode listNode3 = new ListNode(34);
        MySingleList.ListNode listNode4 = new ListNode(45);
        MySingleList.ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display() {
        for(MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            System.out.print(cur.val + " ");
        }

        System.out.println();
    }

    public int size() {
        int size = 0;

        for(MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            ++size;
        }

        return size;
    }

    public boolean contains(int key) {
        for(MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            if (cur.val == key) {
                return true;
            }
        }

        return false;
    }

    public void addFirst(int data) {
        MySingleList.ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data) {
        MySingleList.ListNode node = new ListNode(data);
        MySingleList.ListNode cur = this.head;
        if (cur == null) {
            this.head = node;
        } else {
            while(true) {
                if (cur.next == null) {
                    cur.next = node;
                    break;
                }

                cur = cur.next;
            }
        }

    }

    public void addIndex(int index, int data) {
        if (index >= 0 && index <= this.size()) {
            if (index == 0) {
                this.addFirst(data);
            } else if (index == this.size()) {
                this.addLast(data);
            } else {
                MySingleList.ListNode cur = this.findIndexSubOne(index);
                MySingleList.ListNode node = new ListNode(data);
                node.next = cur.next;
                cur.next = node;
            }
        } else {
            System.out.println("index位置不合法");
            throw new IndexWrongfulException("index位置不合法");
        }
    }

    private MySingleList.ListNode findIndexSubOne(int index) {
        MySingleList.ListNode cur;
        for(cur = this.head; index - 1 != 0; --index) {
            cur = cur.next;
        }

        return cur;
    }

    public void remove(int key) {
        if (this.head == null) {
            System.out.println("链表为空");
        } else if (this.head.val == key) {
            this.head = this.head.next;
        } else {
            MySingleList.ListNode cur = this.findPrevOfKey(key);
            if (cur == null) {
                System.out.println("没有你要删除的数据");
            } else {
                cur.next = cur.next.next;
            }

        }
    }
    private ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    public MySingleList.ListNode removeAllKey(int key) {
        if (this.head == null) {
            return null;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = head.next;
        }
        return head;
    }

    static class ListNode {
        int val;
        MySingleList.ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode FindKthToTail(ListNode head,int k) {
        if (k < 0 || this.head == null) {
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;

        while (k-1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                fast = head;
                while (fast != slow) {
                    fast = fast.next;
                    slow = slow.next;
                }
                return fast;
            }
        }
        return null;
    }

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    //判定链表相交
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode fast = headA;
        ListNode slow = headB;
        if (headA==null || headB == null) {
            return null;
        }

        int fastCount = 0;
        int slowCount = 0;
        while (fast.next != null) {
            fast = fast.next;
            fastCount++;
        }
        while (slow.next != null) {
            slow = slow.next;
            slowCount++;
        }
        if (fast != slow) {
            return null;
        }

        fast = headA;
        slow = headB;
        if (fastCount > slowCount) {
            for (int i = 0; i < fastCount-slowCount; i++) {
                fast = fast.next;
            }
        } else {
            for (int i = 0; i < slowCount-fastCount; i++) {
                slow = slow.next;
            }
        }
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    // 判断是否是回文链表
    public boolean chkPalindrome(ListNode head) {
        // write code here
        // 判断链表是否为空
        if (head == null) {
            return false;
        }
        // 如果链表之中只有一个元素，返回真
        if (head.next == null) {
            return true;
        }
        // 找到链表的中间节点
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        // 反转slow后的链表
        ListNode cur = slow.next;

        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        // 判断回文
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    // 链表分割
    public ListNode partition(ListNode head, int x) {
        // write code here
        // 创建两个傀儡节点
        ListNode node1 = null;
        ListNode node2 = null;
        // 遍历节点 与x比较大小，分别放入两个傀儡节点的后面
        ListNode cur = head;
        ListNode cur1 = null;
        ListNode cur2 = null;
        while (cur != null) {
            if (cur.val < x) {
                if (node1 == null) {
                    node1 = cur;
                    cur1 = cur;
                } else {
                    cur1.next = cur;
                    cur1 = cur1.next;
                }
            } else {
                if (node2 == null) {
                    node2 = cur;
                    cur2 = cur;
                } else {
                    cur2.next = cur;
                    cur2 = cur2.next;
                }
            }
            cur = cur.next;
        }
        if (node1 == null) {
            return node2;
        }
        cur1.next = node2;
        if (node2 != null) {
            cur2.next = null;
        }

        return node1;
    }
}